import { login } from "@/api/login";

const state = {
  name: "",
  currentUser: {}
};

const mutations = {
  SET_NAME: (state, name) => {
    state.name = name;
  },
  SET_USER: (state, user) => {
    state.currentUser = user;
  }
};

const actions = {
  login({ commit, state }, userInfo) {
    return new Promise((resolve, reject) => {
      login(userInfo)
        .then(response => {
          commit("SET_NAME", response.data.loginName);
          console.log(state.name);
          resolve();
        })
        .catch(error => {
          reject();
        });
    });
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
